Skip to content

fix(lib): support JSON Schema type arrays in transform_schema (Closes #1876) - #1892

Open
Synxneuos wants to merge 2 commits into
anthropics:mainfrom
Synxneuos:fix/transform-schema-type-array-1876
Open

fix(lib): support JSON Schema type arrays in transform_schema (Closes #1876)#1892
Synxneuos wants to merge 2 commits into
anthropics:mainfrom
Synxneuos:fix/transform-schema-type-array-1876

Conversation

@Synxneuos

Copy link
Copy Markdown

Summary

Closes #1876

When a JSON Schema specifies type as an array (e.g., {"type": ["string", "null"]} as generated by Pydantic Optional[T] or Zod .nullable()), transform_schema() previously raised an AssertionError due to unmatched type branches in _transform.py.

Changes

  • In src/anthropic/lib/_parse/_transform.py:
    • Added support for list types in transform_schema(), mapping type: [T1, T2] to anyOf: [{"type": T1}, {"type": T2}].
    • Updated type annotations for type_ to Optional[SupportedTypes | list[SupportedTypes]].
    • Allowed is_list(type_) in the terminal validation pass to prevent false assert_never triggers.
  • In tests/lib/_parse/test_transform.py:
    • Added unit tests test_type_array() and test_type_array_in_object().

Verification

  • All 16 unit tests in tests/lib/_parse/test_transform.py passed (16 passed in 6.03s).

@Synxneuos
Synxneuos requested a review from a team as a code owner August 28, 2026 13:59

@sylvesterkaczmarek sylvesterkaczmarek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting a type array from only the type names drops type-specific sibling keywords. For example {'type':['array','null'],'items':{'type':'string'}} becomes an anyOf whose array branch has no items; the original items is only appended to the description, so item type is no longer enforced. The same applies to other type-specific keywords. Could each generated branch be transformed with the applicable sibling schema keywords, or otherwise preserve those constraints?

@Synxneuos

Copy link
Copy Markdown
Author

Thanks for the thorough review @sylvesterkaczmarek! Updated in commit 5d5512d.

We now extract and distribute type-specific sibling keywords (items, minItems for arrays; properties, required, additionalProperties for objects; format for strings; minimum/maximum for numbers/integers) into their respective generated variant branches before applying transform_schema() recursively.

For example, {"type": ["array", "null"], "items": {"type": "string"}, "description": "Optional list"} now cleanly transforms into:

{
  "anyOf": [
    {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    {
      "type": "null"
    }
  ],
  "description": "Optional list"
}

Added unit tests covering type arrays with items, properties, and nested schemas in tests/lib/_parse/test_transform.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transform_schema raises AssertionError on type arrays (e.g. type: ["string","null"])

2 participants